using System.IO;
using System.Linq;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using MarkdownRow.Assets;
using Newtonsoft.Json;

namespace MarkdownRow;

public class App : Application
{
    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted()
    {
        string s;

        if (File.Exists("setting.json"))
        {
            s = File.ReadAllText("setting.json");
        }
        else
        {
            s = JsonConvert.SerializeObject(new Setting());
            File.WriteAllText("setting.json", s);
        }

        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            var a = desktop.Args?.FirstOrDefault();
            desktop.MainWindow = new MainWindow(Doc.Default,
                JsonConvert.DeserializeObject<Setting>(s), a);
        }

        base.OnFrameworkInitializationCompleted();
    }
}